package com.yanggu.spock.service

import com.yanggu.spock.dao.UserDao
import com.yanggu.spock.domain.entity.UserEntity
import com.yanggu.spock.util.IDNumberUtils
import spock.lang.Specification

import static org.mockito.ArgumentMatchers.anyString
import static org.mockito.Mockito.mockStatic

class UserServiceStaticTest extends Specification {

    def userService = new UserService()

    def dao = Mock(UserDao)

    void setup() {
        userService.setUserDao(dao)
    }

    def "GetUserByIdStatic"() {
        given: "设置请求参数"

        def user1 = new UserEntity(id: 1, name: "张三", province: "上海", no: "none")
        def user2 = new UserEntity(id: 2, name: "李四", province: "江苏")
        def idMap = ["birthday": "1992-09-18", "sex": "男", "age": "28"]

        and: "mock掉接口返回的用户信息"
        dao.getUserInfo() >> [user1, user2]

        and: "mock静态方法返回值"
        def mockStatic = mockStatic(IDNumberUtils.class)
        mockStatic.when { IDNumberUtils.getBirAgeSex(anyString()) }.thenReturn(idMap)

        when: "调用获取用户信息方法"
        def response = userService.getUserByIdStatic(1)

        then: "验证返回结果是否符合预期值"
        with(response) {
            name == "张三"
            abbreviation == "沪"
            postCode == 200000
            age == 28
        }

        cleanup: "清理mockStatic"
        mockStatic.close()
    }

}